import navToNext from '../../../wxjs/navToNext.js'
const app = getApp();
var d = new Date()
var y = d.getFullYear()
var m = d.getMonth() + 1
m < 10 ? m = '0' + m : m
var d1 = d.getDate()
d1 < 10 ? d1 = '0' + d1 : d1
var today = `${y}-${m}-${d1}`
var tomorrow = getCurTime(1, today)
var twoDayLater = getCurTime(2, today)
var data = {
  statusBarHeight: app.globalData.statusBarHeight,
  xhx_images: app.xhx_images,
  xhx_route: app.xhx_route,
  date:null,
  challengeType: [{ name: '步数挑战', type: 'step' }, { name: '卡路里挑战', type: 'kcal' }],

  rewardMold:null,
  curType: 'step',
  dayAim:null,
  start:null,
  end:null,
  activeDay: null,
  price:null,
  deductPercentage:null,

  tomorrow:null,
  twoDayLater: null, // 明天的明天
  timeScope: { beginStart: tomorrow, beginEnd: null, overStart: twoDayLater, overEnd:null},
  rules: { dayAim: '每日目标值', startTime: '开始时间', endTime: '结束时间', price: '报名费用'},
};

var currentPage = {
  data,
  onLoad,
  cutType,

  getCurTime,
  addZero,
  bindChangeStart,
  bindChangeEnd,
  getDay,
  getActiveDay,
  getAim,

  getPrice,
  getPercent,

  formSubmit,
};

var page = Object.assign({}, navToNext, currentPage);

Page(page);

function onLoad(options) {
  const that = this;
  var tomorrow = that.getCurTime(1);
  var twoDayLater = that.getCurTime(2);
  var timeScope = that.data.timeScope;
  timeScope.beginStart = tomorrow;
  timeScope.overStart = twoDayLater;
  that.setData({
    rewardMold:options.type,
    tomorrow: tomorrow,
    twoDayLater,
    timeScope: timeScope
  })

  if (4 == options.type) {
    var start = this.data.start = that.getCurTime(1);
    var end = this.data.end = that.getCurTime(2);
    that.setData({
      start,
      end,
      activeDay: 1,
    })
  }
}
// 切换挑战类型
function cutType(e){
  var type = e.currentTarget.dataset.type;
  this.setData({
    curType:type
  })
}
// 时间设置
function getCurTime(num, time){
  var unixTimestamp = null;
  if(time){
    unixTimestamp = new Date(time);
  }else{
    unixTimestamp = new Date();
  }
  // 转化成毫秒数
  unixTimestamp = unixTimestamp.getTime() + num*24 * 60 * 60 * 1000;
  var time = new Date(unixTimestamp);
  
  var oYear = time.getFullYear();
  var oMonth = time.getMonth() + 1;
  var oDay = time.getDate();

  var tomorrow = oYear + '-' + addZero(oMonth) + '-' + addZero(oDay);
  return tomorrow;
}
function addZero(num) {
  if (parseInt(num) < 10) {
    num = '0' + num;
  }
  return num;
} 
function bindChangeStart(e) {
  const that=this;
  var val = e.detail.value;
  var timeScope = that.data.timeScope;

  // 判断一下时间
  if (new Date(val) < new Date(timeScope.beginStart)) {
    val = timeScope.beginStart
  }

  // 修改结束时间的范围
  timeScope.overStart = that.getCurTime(1, val);
  timeScope.overEnd = that.getCurTime(30, val);
  
  that.setData({
    start: val,
    timeScope: timeScope
  })
  that.getDay();
  that.getActiveDay();
}
function bindChangeEnd(e) {
  const that = this;
  var val = e.detail.value;
  var timeScope = that.data.timeScope;
  timeScope.beginStart = that.getCurTime(-30, val);
  // 判断beginStart是否早于today，早于就使用today
  if ((new Date(timeScope.beginStart)).getTime() - (new Date(that.data.tomorrow)).getTime() < 0) {
    timeScope.beginStart = that.data.tomorrow;
  }
  timeScope.beginEnd = that.getCurTime(-1, val);
  // 判断一下时间
  if (new Date(val) <= new Date(that.data.start)) {
    val = timeScope.overStart
  }
  that.setData({
    end: val,
    timeScope: timeScope
  });
  that.getDay();
  that.getActiveDay();
}
function getDay(){
  const that=this;
  if (that.data.start && that.data.end){
    var startStamp = new Date(that.data.start);
    startStamp = startStamp.getTime();
    var endStamp = new Date(that.data.end);
    endStamp = endStamp.getTime();
    var day = (endStamp - startStamp)/(1000*60*60*24);
  }
}
function getActiveDay(){
  const that=this;
  var start = that.data.start;
  var end = that.data.end;
  if (start && end) {
    start = new Date(start);
    start = start.getTime();
    end = new Date(end);
    end = end.getTime();

    var time = (end - start)/(1000*60*60*24);
    // time=time;
    var price=that.data.price;
    if(price){
      var maxPrice = time * 200;
      var minPrice = time * 1;
      if (price > maxPrice) {
        price = maxPrice;

        wx.showToast({
          title: '报名费用每天最高200元，每增加一天可相应增加200元',
          icon: 'none',
          duration: 4000
        });
      } else if (price < minPrice) {
        price = minPrice;
      }
    }
    that.setData({
      activeDay:time,
      price: price
    })
  }
}

function getAim(e){
  const that = this;
  var val = Number.parseInt(e.detail.value);
  if(val<100){
    val=100;
  }
  that.setData({
    dayAim: val
  })
}

function getPrice(e){
  const that=this;
  var val = e.detail.value;
  var day=that.data.activeDay;
  if(day){
    var maxPrice=day*200;
    var minPrice=day*1;
    if(val>maxPrice){
      val=maxPrice;
    } else if (val < minPrice){
      val = minPrice;
    }
  }
  that.setData({
    price: val
  })
}

function getPercent(e){
  const that = this;
  var val = e.detail.value;
  if (val>10) {
    val=10;
    that.setData({
      deductPercentage: val
    })
  }
}

function formSubmit(e) {
  const that=this;
  console.log('form发生了submit事件，携带数据为：', e.detail.value)
  var form = e.detail.value;
  if (!form.deductPercentage) form.deductPercentage ='0';

  for (var key in form){
    if (!form[key]){
      var title = that.data.rules[key]+'不为空';
      wx.showToast({
        title: title,
        icon: 'none',
        duration: 3000
      });
      return;
    }
  }

  if (this.data.activeDay > 31) {
    wx.showToast({
      title: '活动天数最多 31 天',
      icon: 'none',
      duration: 3000
    });
    return
  }
  form.matchWay = that.data.curType;
  form.rewardMold = that.data.rewardMold;
  app.createChallegeParam=form;

  wx.navigateTo({
    url: that.data.xhx_route.creatChallengeName,
  })

}